package com.carolouvindam;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.carolouvindam.Model.Order;
import com.carolouvindam.database.DataSource;

public class OrderActivity extends Activity {
    /**
     * Called when the activity is first created.
     */

    public ListView lv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // On met le XML
        setContentView(R.layout.orderlist);

        // Affiche la listview
        refreshListView();

        // Ajout du listener pour les touch simple à la list view
        lv.setOnItemClickListener(simpleTouchListenner);

        // Ajout du menu pour les touch long
        registerForContextMenu(lv);

    }


    private ListView.OnItemClickListener simpleTouchListenner = new ListView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            // Petit toast
            Toast toast = Toast.makeText(getBaseContext(), "Displaying the order of " + ((Order) (adapterView.getItemAtPosition(i))).getClient().getName(), Toast.LENGTH_SHORT);
            toast.show();

            viewOrder((Order) adapterView.getItemAtPosition(i));
        }
    };


    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {


        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();


        switch (item.getItemId()) {
            case R.id.button_view:
                Toast.makeText(this, "Displaying the order.",
                        Toast.LENGTH_SHORT).show();
                viewOrder((Order) lv.getItemAtPosition((int) info.id));
                return true;
            case R.id.button_delete:
                setAlertBeforeDelete((Order) lv.getItemAtPosition((int) info.id));
                return true;
            default:

                return super.onContextItemSelected(item);
        }


    }


    public void LaunchCreateOrder(View view) {
        Intent intent = new Intent(this, FormActivity.class);
        startActivity(intent);
    }

    private void viewOrder(Order order) {
        Intent intent = new Intent(this, OrderSheetActivity.class);
        intent.putExtra("orderIdDB", Integer.parseInt(order.getIdDB().toString()));
        startActivity(intent);
    }

    private void setAlertBeforeDelete(final Order orderToDelete) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Are you sure you want to delete this order? It can't be undone.")
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        DataSource dataSource = new DataSource(getBaseContext());
                        dataSource.open();
                        dataSource.delete(orderToDelete);
                        dataSource.close();
                        refreshListView();
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }

    private void refreshListView() {
        DataSource dataSource = new DataSource(this);
        dataSource.open();
        ListAdapter adapter = new ListViewOrderAdapter(this, R.layout.listitemorder, dataSource.getAllOrderByTime());

        lv = (ListView) findViewById(R.id.ListView);
        lv.setAdapter(adapter);
        lv.setTextFilterEnabled(true);
        lv.setItemsCanFocus(false);
        dataSource.close();

    }


}
